package com.yizhubao.app.customviews;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.yizhubao.app.R;
import com.yizhubao.app.activitys.ExplainActivity;
import com.yizhubao.app.activitys.WebActivity;
import com.yizhubao.app.customviews.inter.CompleteCallBack;
import com.yizhubao.app.customviews.inter.OthersCallBack;
import com.yizhubao.app.utils.Constants;

/**
 * 创建人: PACY
 * 创建时间: 6/7/21 3:54 PM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 弹窗封装
 */
public class AgreementDialog {
    private Context mContext;
    private CompleteCallBack completeCallBack;
    private OthersCallBack othersCallBack;


    public AgreementDialog(@NonNull Context context,
                           CompleteCallBack completeCallBack, OthersCallBack othersCallBack) {
        this.mContext = context;
        this.completeCallBack=completeCallBack;
        this.othersCallBack=othersCallBack;
    }

    public AgreementDialog(@NonNull Context context) {
        this.mContext = context;
    }

    public void show() {
        View view = View.inflate(mContext, R.layout.agreement_dialog_layout, null);
        BottomSheetDialog dialog = new BottomSheetDialog(mContext, R.style.BottomSheetDialog);
        dialog.setCancelable(true);
        TextView twoBtnCopmlete_tv=view.findViewById(R.id.twoBtnCopmlete_tv);
        TextView twoBtnCancle_tv=view.findViewById(R.id.twoBtnCancle_tv);
        TextView content=view.findViewById(R.id.content);
        initContactText(content);
        twoBtnCopmlete_tv.setOnClickListener(v -> {
            if(completeCallBack!=null){
                completeCallBack.onComplete();
            }
            dialog.dismiss();
        });

        twoBtnCancle_tv.setOnClickListener(v -> {
            if(othersCallBack!=null){
                othersCallBack.onOther();
            }
            dialog.dismiss();
        });
        dialog.setContentView(view);
        dialog.show();
    }

    public static Builder DialogBuilder(Context context) {
        Builder build = new Builder(context);
        return build;
    }

    /**
     * 初始化联系客服文本
     */
    private void initContactText(TextView content){
        String str="您的所有信息，都将通过军工级别的加密方式，进行保存。在您使用遗嘱宝APP之前，请认真阅读《隐私政策》和《用户协议》全部条款。您同意全部条款后，再开始使用我们的服务。";
        SpannableStringBuilder span = new SpannableStringBuilder(str);
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Bundle bundle=new Bundle();
                bundle.putString("title","隐私政策");
                bundle.putString("url", "https://resource.weimingfang.com/pages/yinsizhengce.html");
                Intent intent=new Intent(mContext, WebActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(mContext.getResources().getColor(R.color.btn_color_blue));
            }
        },44,50, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Bundle bundle=new Bundle();
                bundle.putString("title","用户协议");
                bundle.putString("url", "https://resource.weimingfang.com/pages/yonghuxieyi.html");
                Intent intent=new Intent(mContext, WebActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(mContext.getResources().getColor(R.color.btn_color_blue));
            }
        },51,57, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        content.setMovementMethod(LinkMovementMethod.getInstance());
        content.setText(span);
    }


    public static class Builder {
        private Context context;
        private CompleteCallBack completeCallBack;
        private OthersCallBack othersCallBack;

        public Builder(Context context) {
            this.context = context;
        }


        public Builder completeCallBack(CompleteCallBack completeCallBack) {
            this.completeCallBack = completeCallBack;
            return this;
        }

        public Builder othersCallBack(OthersCallBack othersCallBack) {
            this.othersCallBack = othersCallBack;
            return this;
        }



        public void show() {
            AgreementDialog dialog = new AgreementDialog(context,completeCallBack,othersCallBack);
            dialog.show();
        }
    }
}
